/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package daoimpl;

import controller.ConnectionMySQL;
import dao.PacienteDao;
import enun.EstadoCivil;
import enun.Sexo;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Endereco;
import model.Paciente;

/**
 *
 * @author Rodrigo
 */
public class PacienteDaoImpl implements PacienteDao{

   @Override
    public void inserir(Paciente p) {

        String sql = "INSERT INTO Paciente (nome,idade,sexo,estadocivil,id_endereco) values (?,?,?,?,?)";
        try {
            PreparedStatement ps = ConnectionMySQL.getConnection().prepareStatement(sql);

            ps.setString(1, p.getNome());
            ps.setInt(2, p.getIdade());
            ps.setString(3, p.getSexo().name());
            ps.setString(4, p.getEstadoCivil().name());
            ps.setInt(5, p.getEndereco().getId());

            ps.execute();

            System.out.println("Dados Inseridos");

        } catch (SQLException e) {
            System.out.println("ERROR !");
           
        }
    }

    @Override
    public void alterar(Paciente p) {

        String sql = "update Paciente set nome = ?, idade = ?, sexo = ?, estadocivil = ? , id_endereco = ? where id = ?";
        try {
            PreparedStatement ps = ConnectionMySQL.getConnection().prepareStatement(sql);

            ps.setString(1, p.getNome());
            ps.setInt(2, p.getIdade());
            ps.setString(3, p.getSexo().name());
            ps.setString(4, p.getEstadoCivil().name());
            ps.setInt(5, p.getEndereco().getId());
            ps.setInt(6, p.getId());
            
            ps.execute();

            System.out.println("Dados Inseridos");

        } catch (SQLException e) {
            System.out.println("ERROR !");
          
        }

    }

    @Override
    public List<Paciente> listarTodos() {
        StringBuilder sb = new StringBuilder(); 
        
        sb.append(" SELECT p.id, p.nome,p.idade,p.sexo,p.estadocivil,e.logadouro,e.cidade,e.numero, ");
        sb.append(" e.bairro, e.cep, e.idendereco FROM Paciente p");
        sb.append(" INNER JOIN Endereco e on p.id_endereco = e.idendereco");     
       
        List<Paciente> pacientes = new ArrayList<>();
        try {
            PreparedStatement ps = ConnectionMySQL.getConnection().prepareStatement(sb.toString());
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                Paciente p = new Paciente();
                Endereco e = new Endereco();              
                p.setEndereco(e);
                p.setId(rs.getInt("p.id"));
                p.setNome(rs.getString("p.nome"));
                p.setIdade(rs.getInt("p.idade"));
                p.setSexo(Sexo.valueOf(rs.getString("p.sexo")));
                p.setEstadoCivil(EstadoCivil.valueOf(rs.getString("p.estadocivil")));
                p.getEndereco().setLogadouro(rs.getString("e.logadouro"));
                p.getEndereco().setCidade(rs.getString("e.cidade"));
                p.getEndereco().setNumero(rs.getString("e.numero"));
                p.getEndereco().setBairro(rs.getString("e.bairro"));                
                e.setId(rs.getInt("e.idendereco")); 
                e.setCep(rs.getString("e.cep"));
                pacientes.add(p);
            }

        } catch (SQLException e) {
            System.out.println("ERROR !");
        }

        return pacientes;
    }

    
   @Override
    public Paciente listarPorId(int id) {
           String sql = "select * from Paciente where id=?";
           Paciente p = new Paciente();
        try {
            PreparedStatement ps = ConnectionMySQL.getConnection().prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                
                p.setId(rs.getInt("id"));
                p.setNome(rs.getString("nome"));
                p.setIdade(rs.getInt("idade"));
                p.setSexo(Sexo.valueOf(rs.getString("sexo")));
                p.setEstadoCivil(EstadoCivil.valueOf(rs.getString("estadocivil")));               
            }

        } catch (SQLException e) {
            System.out.println("ERROR !");
            
        }

        return p;

    }

    @Override
    public void deletar(Paciente p) {
        String sql = "delete from Paciente where id = ?";
        try {
            PreparedStatement ps = ConnectionMySQL.getConnection().prepareStatement(sql);
            ps.setInt(1, p.getId());
            ps.execute();

        } catch (SQLException e) {
            System.out.println("ERROR !");
            
        }
    }
  
}
